package net.app.zoneland.db.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import net.app.zoneland.db.handler.MethodHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Description mybatis配置
 * @Author cjb
 * @CreateTime 2020/12/13 10:35
 * @Version: 1.0
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MateMybatisPlusConfig {

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

   /* @Bean("scootSqlSessionFactory")
    public SqlSessionFactory scootSqlSessionFactory(DataSource scottDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(scottDataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("/mapping/*.xml"));
        //下面这一行！！！
        sessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor()});//这一行！！！
        //上面这一行！！！
        return sessionFactoryBean.getObject();
    }*/

    /**
     * 自动填充数据库创建人，创建时间，更新人，更新时间
     * @return
     */
    @Bean
    public GlobalConfig globalConfig(){
        GlobalConfig globalConfig=new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MethodHandler());
        return globalConfig;
    }
}
